<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
    <meta charset="UTF-8">
    <title>推送绑定</title>
    <link rel="stylesheet" href="__CDN__/assets/css/weui.min.css">
    <link rel="stylesheet" href="__CDN__/assets/css/jquery-weui.css">
    <link rel="stylesheet" href="__CDN__/assets/css/register.css">
</head>
<body ontouchstart>
<!--主体-->
<div class="login-box">
    <div class="register">
        <div class="inner">
            <h1 class="ImgTitle">百狮人力</h1>

            <div class="wrapper">
                <div class="code">
                    <div class="weui-cells weui-cells_form form-control">
                        
                        <div class="weui-cell">
                            <input id="openid" type="hidden" value="{$openid}"/>
                            <!--<div class="weui-cell__hd"><label class="weui-label" style="width:90px">手机号码</label></div>-->
                            <div class="weui-cell__bd">
                                <input class="weui-input phoneNum" type="number" pattern="[0-9]*" placeholder="请输入手机号码">
                            </div>
                        </div>
                        <div class="weui-cell">
                            <!--<div class="weui-cell__hd"><label class="weui-label" style="width:90px">验证码</label></div>-->
                            <div class="weui-cell__bd">
                                <input class="weui-input verify_code" type="number" pattern="[0-9]*" placeholder="验证码">
                            </div>
                            <div class="weui-cell__ft">
                                <button id="get-btn">获取验证码</button>
                            </div>
                        </div>



                    </div>
                    <div class="btn-box">
                        <button type="button" class="btn btn-orange-fill" id="register">立即绑定</button>

                    </div>
                </div>
            </div>

        </div>
    </div>
</div>



<script src="__CDN__/assets/js/jquery-2.1.4.js"></script>
<script src="__CDN__/assets/js/jquery-weui.js"></script>
<script>
    function GetQueryString(name) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return unescape(r[2]);
        }
        return '';
    }

    $(document).ready(function() {

        $(".btn-orange-hollow").click(function () {
            location.href="./download.html";
        });
		$("#xieyi").click(function () {
            location.href="./agree.html";
        });

        var referPhone = '';
        var refer = GetQueryString("referPhone");
        if(refer != '') {
			$("#tjr").val(refer);
            if(refer.length <= 10){
                $(".referPhone").show();
            }
        } else {
            $(".referPhone").show();
        }

        $("#get-btn").click(function () {
            var myreg=/^[1][3,4,5,6,7,8,9][0-9]{9}$/;
            var phonenum = $(".phoneNum").val();
            if (!myreg.test(phonenum)) {
                $.toast("手机号格式不正确", "cancel");
                return false;
            }
            var data = JSON.stringify({
                "mobile": phonenum,
            });
            $.ajax({
                type:"post",
                headers:{
                    'Content-Type':'application/json'
                },
                url:"/api/common/sendMessage",
                data:data,
                dataType:"json",
                success:function(result) {
                    if (result.code == 1) {
                        $.toast('获取验证码成功');
                        var code = $("#get-btn");
                        console.log(code);
                        code.attr("disabled","disabled");
                        console.log(code);
                        setTimeout(function(){
                            code.css("opacity","0.5");
                        },1000)
                        var time = 60;
                        var set=setInterval(function(){
                            code.text("("+--time+")秒后重新获取");
                        }, 1000);
                        setTimeout(function(){
                            code.attr("disabled",false).text("获取验证码");
                            clearInterval(set);
                        }, 60000);
                    } else {
                        $.toast("获取验证码失败", "cancel");
                    }
                }
            });
        });

        $('#download').click(function () {
            window.location.href = 'http://happy.50xue.com/index/index/download ';
        });

        $('#register').click(function() {

            var data = JSON.stringify({
                "phone": $(".phoneNum").val(),
                "code": $('.verify_code').val(),
                "openid":$("#openid").val(),
            });

            $.ajax({
                'url': '/api/user/bind',
				'headers': {
                    'Content-Type': 'application/json'
                },
                data:data,
                'type': 'POST',
                'dataType': 'json',
                'success': function (res) {
                    if (res.code == 1) {
                        $.toast("绑定成功", "text");
                    } else {
                        $.toast(res.msg, "cancel");
                    }
                },
                'error': function(XMLHttpRequest, textStatus) {
                    var $responseText = $.parseJSON(XMLHttpRequest.responseText);
                    console.log(XMLHttpRequest.status);
                    console.log($responseText);
                }
            });
        });

//        function getQueryVariable(variable)
//        {
//            var query = window.location.search.substring(1);
//            var vars = query.split("&");
//            for (var i=0;i<vars.length;i++) {
//                var pair = vars[i].split("=");
//                if(pair[0] == variable) {
//                    return pair[1];
//                }
//            }
//            return '';
//        }
    })

</script>
</body>
</html>